package com.ophiux.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
	private StringRedisTemplate stringRedisTemplate;
	
	public void setString(String key,String value) {
		this.setObject(key, value);
	}
	
	public void setList(String key,String listValue) {
		this.setObject(key, listValue);
	}
	
	
	public void setObject(String key,Object value) {
		//redis有几种数据类型 String list set zset hash
		if(StringUtils.isEmpty(key)||value==null) {
			return ;
		}
		//判断类型
		if(value instanceof String) {
			String strValue = (String) value;
			stringRedisTemplate.opsForValue().set(key, strValue);
			return;
		}
		
		if(value instanceof List) {
			List<String> listValue = (List<String>) value;
			for (String string : listValue) {
				stringRedisTemplate.opsForList().leftPush(key, string);
			}
			return;
		}
	}
	
}
